package com.car.action.api;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.compress.utils.Lists;

import com.alibaba.fastjson.JSONArray;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.car.common.Tidy;
import com.car.common.model.Area;
import com.car.common.model.CardDiscuss;
import com.car.common.model.NavyUsers;
import com.car.common.model.Users;
import com.car.common.model.UsersInfo;
import com.google.common.collect.Maps;
import com.jfinal.core.Controller;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.IDataLoader;
import com.utils.APIJSON;
import com.utils.AliSMSUtil;
import com.utils.DateUtil;
import com.utils.RongCloudUtil;
import com.utils.StringUtil;

public class AreaController extends Controller {

	public void list() {
		Tidy tidy = new Tidy(Tidy.getCurrentClassAndMethodNames(Tidy.CUR_METHOD_STACK), getRequest());
		
		ArrayList<Area> areas = CacheKit.get("constant", "arealist", new IDataLoader(){
		       public Object load() {
		           return Area.dao.getIProvince();
		}});
	    tidy.addData("data", areas);
	    renderJson(tidy.putFinalReturn(0));
	}
	
	public void get_province() {
		ArrayList<Area> areas = CacheKit.get("constant", "arealist", new IDataLoader(){
		       public Object load() {
		           return Area.dao.getIProvince();
		}});
	    renderText( APIJSON.send(areas));
	}
	
	public void test_x() {
		/*
		List<Users> users  = Users.dao.find("select id,user_mobile from users where user_mobile is not null and user_mobile !='null'  and id > 202273 and id < 600000 order by id asc limit 40000 ");
		for(Users u:users) {
			System.out.println("update users set user_mobile = '" + u.getUserMobile() +"' where user_mobile is null and id = " + u.getId() +";");
		}*/
		
		List<Users> users  = Users.dao.find("select id,user_mobile from users where user_mobile is null and is_true = 3 order by id asc limit 40000 ");
		Long l =  88800000001L;
		for(Users u:users) {
			System.out.println("update users set user_mobile = '" + l +"' where user_mobile is null and id = " + u.getId() +";");
			l++;
		}
		renderText("OK");
	}
	
	public void test() {
		List<Users> users  = Users.dao.find("select * from users where is_true > 1 and sex = 2 and zu is null ");
		for (Users one : users ) {
			UsersInfo info = UsersInfo.dao.findFirst("select * from users_info where user_id = " + one.getId() +" order by id limit 1 ");
			if (info != null) {
				template(0, one, info);
			}
			
		}
		/*
		List<CardDiscuss> discussList = CardDiscuss.dao.find("Select u.area_id,u.sex,d.content from card_discuss as d LEFT JOIN users as u on u.id = d.user_id  limit 5");
		List<Map<String,Object>> paramList = Lists.newArrayList();
		for(CardDiscuss d :discussList) {
			Map<String,Object> temp = Maps.newHashMap();
			temp.put("area_id", d.get("area_id"));
			temp.put("sex", d.getInt("sex"));
			temp.put("content", StringUtil.decode(d.getStr("content")));
			paramList.add(temp);
		}
		System.out.println(JSONArray.toJSONString(paramList));
		/*
		List<Users> users = Users.dao.find("select id from users where is_true = 1 and sex = 1 and vip_badpe < 4 and ( login_status = 0 or logout_time > 1572599775 )");
		
		for(Users one : users) {
			RongCloudUtil.sendTxt("200", String.valueOf(one.getId()) , "亲爱的XXX，刚刚发现你购买会员没有成功，是不是支付遇到问题了？要不要试试用手机浏览器打开官方网站www.50duhui.top，1分钟搞定~（至尊永久会员仅官网限时发售）");
		}
		*/
		renderText("OK");
	}
	
	/**
	 * 
	 * @param template   0 随机
	 * @param users
	 */
	private void template(int template ,Users users ,UsersInfo info) {
		if(template == 0) {
			template = 1 + new Random().nextInt(40);
		}
		try {
			if (template == 1) {
				users.setTagIds("70,72,112,86,102,106,96,134,141,140");
				users.setBirthday(DateUtil.standardDateFormat.parse("1997/06/22").getTime()/1000L);
				info.setHeight(166);
				info.setWeight(47);
				info.setOrientation(1);
				info.setEmotion(2);
			} else if (template == 2) {
				users.setTagIds("70,72,112,92,99,83,100,138,141,128");
				users.setBirthday(DateUtil.standardDateFormat.parse("2000/08/22").getTime()/1000L);
				info.setHeight(165);
				info.setWeight(47);
				info.setOrientation(3);
				info.setEmotion(2);
			} else if (template == 3) {
				users.setTagIds("70,72,112,109,127,135,123,133");
				users.setBirthday(DateUtil.standardDateFormat.parse("1998/04/13").getTime()/1000L);
				info.setHeight(164);
				info.setWeight(45);
				info.setOrientation(3);
				info.setEmotion(2);
			} else if (template == 4) {
				users.setTagIds("70,72,112,92,86,126,123,140,134");
				users.setBirthday(DateUtil.standardDateFormat.parse("1999/08/005").getTime()/1000L);
				info.setHeight(167);
				info.setWeight(48);
				info.setOrientation(1);
				info.setEmotion(3);
			// 70,72,112,106,83,143,140	1996/10/05					164	47	2		1	2
			} else if (template == 5) {
				users.setTagIds("70,72,112,106,83,143,140");
				users.setBirthday(DateUtil.standardDateFormat.parse("1996/10/05").getTime()/1000L);
				info.setHeight(164);
				info.setWeight(47);
				info.setOrientation(1);
				info.setEmotion(2);
			// 70,72,112,96,125,128	1997/01/17					167	48	2		1	2
			} else if (template == 6) {
				users.setTagIds("70,72,112,96,125,128");
				users.setBirthday(DateUtil.standardDateFormat.parse("1997/01/17").getTime()/1000L);
				info.setHeight(167);
				info.setWeight(48);
				info.setOrientation(1);
				info.setEmotion(2);
			// 70,72,112,127,141,126	1997/11/23					165	47	2		1	2
			} else if (template == 7) {
				users.setTagIds("70,72,112,127,141,126");
				users.setBirthday(DateUtil.standardDateFormat.parse("1997/11/23").getTime()/1000L);
				info.setHeight(165);
				info.setWeight(47);
				info.setOrientation(1);
				info.setEmotion(2);
			// 70,72,112,99,100,133,128	1996/01/15					171	54	2		1	3
			} else if (template == 8) {
				users.setTagIds("70,72,112,99,100,133,128");
				users.setBirthday(DateUtil.standardDateFormat.parse("1996/01/15").getTime()/1000L);
				info.setHeight(171);
				info.setWeight(54);
				info.setOrientation(1);
				info.setEmotion(3);
			// 70,72,86,96,123,134	1998/09/11					166	48	2		1	2
			} else if (template == 9) {
				users.setTagIds("70,72,86,96,123,134");
				users.setBirthday(DateUtil.standardDateFormat.parse("1998/09/11").getTime()/1000L);
				info.setHeight(166);
				info.setWeight(48);
				info.setOrientation(1);
				info.setEmotion(2);
			// 70,72,96,83,135,129,130,125	1997/09/30					168	49	2		3	2
			} else if (template == 10) {
				users.setTagIds("70,72,96,83,135,129,130,125");
				users.setBirthday(DateUtil.standardDateFormat.parse("1997/09/30").getTime()/1000L);
				info.setHeight(168);
				info.setWeight(49);
				info.setOrientation(3);
				info.setEmotion(2);
			// 70,72,102,97,108,142,131	1996/06/22					172	56	2		1	2
			} else if (template == 11) {
				users.setTagIds("70,72,102,97,108,142,131");
				users.setBirthday(DateUtil.standardDateFormat.parse("1996/06/22").getTime()/1000L);
				info.setHeight(172);
				info.setWeight(56);
				info.setOrientation(1);
				info.setEmotion(2);
//				70,72,108,122,146,109	1999/08/22					163	43	2		1	2
//				70,72,112,83,96,108,131,118,135	1996/04/13					164	45	2		3	2

			} else if (template == 12) {
				users.setTagIds("70,72,108,122,146,109");
				users.setBirthday(DateUtil.standardDateFormat.parse("1999/08/22").getTime()/1000L);
				info.setHeight(163);
				info.setWeight(43);
				info.setOrientation(1);
				info.setEmotion(2);
			} else if (template == 13) {
				users.setTagIds("70,72,112,83,96,108,131,118,135");
				users.setBirthday(DateUtil.standardDateFormat.parse("1996/04/13").getTime()/1000L);
				info.setHeight(164);
				info.setWeight(45);
				info.setOrientation(3);
				info.setEmotion(2);
			// 70,72,86,99,126,146	1999/08/05					165	44	2		1	3
			// 70,72,112,86,102,96,83,134,135,140	1995/10/05					169	49	2		1	2
			} else if (template == 14) {
				users.setTagIds("70,72,86,99,126,146");
				users.setBirthday(DateUtil.standardDateFormat.parse("1999/08/05").getTime()/1000L);
				info.setHeight(165);
				info.setWeight(44);
				info.setOrientation(1);
				info.setEmotion(3);
			} else if (template == 15) {
				users.setTagIds("70,72,112,86,102,96,83,134,135,140");
				users.setBirthday(DateUtil.standardDateFormat.parse("1995/10/05").getTime()/1000L);
				info.setHeight(169);
				info.setWeight(49);
				info.setOrientation(1);
				info.setEmotion(2);
			//70,72,112,86,102,96,83,134,135,140,133,125	2000/01/17					167	48	2		1	2
			//70,72,113,99,94,144,129	1997/11/23					162	42	2		1	2
			} else if (template == 16) {
				users.setTagIds("70,72,112,86,102,96,83,134,135,140,133,125");
				users.setBirthday(DateUtil.standardDateFormat.parse("2000/01/17").getTime()/1000L);
				info.setHeight(167);
				info.setWeight(48);
				info.setOrientation(1);
				info.setEmotion(3);
			} else if (template == 17) {
				users.setTagIds("70,72,113,99,94,144,129");
				users.setBirthday(DateUtil.standardDateFormat.parse("1997/11/23").getTime()/1000L);
				info.setHeight(162);
				info.setWeight(42);
				info.setOrientation(1);
				info.setEmotion(2);
			// 70,72,127,138,144,133,122,108	1996/01/15					171	50	2		1	3
			// 70,72,92,113,94,134,131,135,144	2000/09/11					166	48	2		1	2
			} else if (template == 18) {
				users.setTagIds("70,72,127,138,144,133,122,108");
				users.setBirthday(DateUtil.standardDateFormat.parse("1996/01/15").getTime()/1000L);
				info.setHeight(171);
				info.setWeight(50);
				info.setOrientation(1);
				info.setEmotion(3);
			} else if (template == 19) {
				users.setTagIds("70,72,92,113,94,134,131,135,144");
				users.setBirthday(DateUtil.standardDateFormat.parse("2000/09/11").getTime()/1000L);
				info.setHeight(166);
				info.setWeight(48);
				info.setOrientation(1);
				info.setEmotion(2);
			//	70,72,112,83,142,133,140	1998/09/30					170	49	2		3	2
			//	69,71,88,110,96,97,102,135,122,133,131,144,125,123,118	1995/06/22					166	47	2		1	2
			} else if (template == 20) {
				users.setTagIds("70,72,112,83,142,133,140");
				users.setBirthday(DateUtil.standardDateFormat.parse("1998/09/30").getTime()/1000L);
				info.setHeight(170);
				info.setWeight(49);
				info.setOrientation(3);
				info.setEmotion(2);
			} else if (template == 21) {
				users.setTagIds("69,71,88,110,96,97,102,135,122,133,131,144,125,123,118");
				users.setBirthday(DateUtil.standardDateFormat.parse("1995/06/22").getTime()/1000L);
				info.setHeight(166);
				info.setWeight(47);
				info.setOrientation(1);
				info.setEmotion(2);
			// 69,71,88,110,96,97,102,135,122,131,144,125,123	1994/08/22					165	47	2		1	2
			//	69,71,88,110,96,97,122,133,131,144,118	1996/04/13					169	48	2		1	2
			//	69,71,88,110,96,97,102,122,133,131,144,125	1996/08/05					170	49	2		1	2
			//	69,71,88,110,96,97,102,135,95,133,131,144,125,123,146	1995/10/05					164	47	2		1	2
			} else if (template == 22) {
				users.setTagIds("69,71,88,110,96,97,102,135,122,131,144,125,123");
				users.setBirthday(DateUtil.standardDateFormat.parse("1994/08/22").getTime()/1000L);
				info.setHeight(165);
				info.setWeight(47);
				info.setOrientation(1);
				info.setEmotion(2);
			} else if (template == 23) {
				users.setTagIds("69,71,88,110,96,97,122,133,131,144,118");
				users.setBirthday(DateUtil.standardDateFormat.parse("1996/04/13").getTime()/1000L);
				info.setHeight(169);
				info.setWeight(48);
				info.setOrientation(1);
				info.setEmotion(2);
			} else if (template == 24) {
				users.setTagIds("69,71,88,110,96,97,102,122,133,131,144,125");
				users.setBirthday(DateUtil.standardDateFormat.parse("1996/08/05").getTime()/1000L);
				info.setHeight(170);
				info.setWeight(49);
				info.setOrientation(1);
				info.setEmotion(2);
			} else if (template == 25) {
				users.setTagIds("69,71,88,110,96,97,102,135,95,133,131,144,125,123,146");
				users.setBirthday(DateUtil.standardDateFormat.parse("1995/10/05").getTime()/1000L);
				info.setHeight(164);
				info.setWeight(47);
				info.setOrientation(1);
				info.setEmotion(2);
			}
			// 69,71,88,110,96,97,106,135,122,133,99,125,123,142	1997/01/17					167	48	2
//			69,71,88,110,96,97,108,135,122,133,131,144,125,123	1997/11/23					165	46	2
//			69,71,88,110,97,108,135,122,133,131,144,125,123	1996/01/15					173	57	2
//			69,71,110,96,97,108,135,144,125,123	1998/09/11					166	48	2
//			69,71,110,96,108,131,135	1997/09/30					168	49	2
			  else if (template == 26) {
					users.setTagIds("69,71,88,110,96,97,106,135,122,133,99,125,123,142");
					users.setBirthday(DateUtil.standardDateFormat.parse("1997/01/17").getTime()/1000L);
					info.setHeight(167);
					info.setWeight(48);
					info.setOrientation(1);
					info.setEmotion(2);
			} else if (template == 27) {
				users.setTagIds("69,71,88,110,96,97,108,135,122,133,131,144,125,123");
				users.setBirthday(DateUtil.standardDateFormat.parse("1997/11/23").getTime()/1000L);
				info.setHeight(165);
				info.setWeight(46);
				info.setOrientation(1);
				info.setEmotion(2);
			} else if (template == 28) {
				users.setTagIds("69,71,88,110,97,108,135,122,133,131,144,125,123");
				users.setBirthday(DateUtil.standardDateFormat.parse("1996/01/15").getTime()/1000L);
				info.setHeight(173);
				info.setWeight(57);
				info.setOrientation(1);
				info.setEmotion(2);
			} else if (template == 29) {
				users.setTagIds("69,71,110,96,97,108,135,144,125,123");
				users.setBirthday(DateUtil.standardDateFormat.parse("1998/09/11").getTime()/1000L);
				info.setHeight(166);
				info.setWeight(48);
				info.setOrientation(1);
				info.setEmotion(2);
			} else if (template == 30) {
				users.setTagIds("69,71,110,96,108,131,135");
				users.setBirthday(DateUtil.standardDateFormat.parse("1997/09/30").getTime()/1000L);
				info.setHeight(168);
				info.setWeight(49);
				info.setOrientation(1);
				info.setEmotion(2);
			}
			// 69,71,88,97,108,134,144	1995/06/22					166	47
			// 69,71,88,94,97,106,135,123,143,125	1994/08/22					165	48
			// 69,71,88,110,113,106,146,122	1996/04/13					169	48
			// 69,71,110,103,97,112,135,122,133,131	1996/08/05					170	49
			// 69,71,88,110,103,96,114,117,77,140	1995/10/05					164	47
			 else if (template == 31) {
				users.setTagIds("69,71,88,97,108,134,144");
				users.setBirthday(DateUtil.standardDateFormat.parse("1995/06/22").getTime()/1000L);
				info.setHeight(166);
				info.setWeight(47);
				info.setOrientation(1);
				info.setEmotion(2);
			} else if (template == 32) {
				users.setTagIds("69,71,88,94,97,106,135,123,143,125");
				users.setBirthday(DateUtil.standardDateFormat.parse("1994/08/22").getTime()/1000L);
				info.setHeight(165);
				info.setWeight(48);
				info.setOrientation(1);
				info.setEmotion(2);
			} else if (template == 33) {
				users.setTagIds("69,71,88,110,113,106,146,122");
				users.setBirthday(DateUtil.standardDateFormat.parse("1996/04/13").getTime()/1000L);
				info.setHeight(169);
				info.setWeight(48);
				info.setOrientation(1);
				info.setEmotion(2);
			} else if (template == 34) {
				users.setTagIds("69,71,110,103,97,112,135,122,133,131");
				users.setBirthday(DateUtil.standardDateFormat.parse("1996/08/05").getTime()/1000L);
				info.setHeight(170);
				info.setWeight(49);
				info.setOrientation(1);
				info.setEmotion(2);
			} else if (template == 35) {
				users.setTagIds("69,71,88,110,103,96,114,117,77,140");
				users.setBirthday(DateUtil.standardDateFormat.parse("1995/10/05").getTime()/1000L);
				info.setHeight(164);
				info.setWeight(47);
				info.setOrientation(1);
				info.setEmotion(2);
			}
			// 69,71,110,129,116,146	1997/01/17					167	48
			// 69,71,110,96,97,108,121,122,132	1997/11/23					165	47
			// 69,71,88,110,96,97,137,145,106	1996/01/15					173	52
			// 69,71,88,110,96,97,108,105,103,106,122,133	1998/09/11					166	48
			// 69,71,88,110,96,97,102,114,122,83,131,144,125,123	1997/09/30					168	49
			 else if (template == 36) {
				users.setTagIds("69,71,110,129,116,146");
				users.setBirthday(DateUtil.standardDateFormat.parse("1997/01/17").getTime()/1000L);
				info.setHeight(167);
				info.setWeight(48);
				info.setOrientation(1);
				info.setEmotion(2);
			} else if (template == 37) {
				users.setTagIds("69,71,110,96,97,108,121,122,132");
				users.setBirthday(DateUtil.standardDateFormat.parse("1997/11/23").getTime()/1000L);
				info.setHeight(165);
				info.setWeight(47);
				info.setOrientation(1);
				info.setEmotion(2);
			} else if (template == 38) {
				users.setTagIds("69,71,88,110,96,97,137,145,106");
				users.setBirthday(DateUtil.standardDateFormat.parse("1996/01/15").getTime()/1000L);
				info.setHeight(173);
				info.setWeight(52);
				info.setOrientation(1);
				info.setEmotion(2);
			} else if (template == 39) {
				users.setTagIds("69,71,88,110,96,97,108,105,103,106,122,133");
				users.setBirthday(DateUtil.standardDateFormat.parse("1998/09/11").getTime()/1000L);
				info.setHeight(173);
				info.setWeight(52);
				info.setOrientation(1);
				info.setEmotion(2);
			} else if (template == 40) {
				users.setTagIds("69,71,88,110,96,97,102,114,122,83,131,144,125,123");
				users.setBirthday(DateUtil.standardDateFormat.parse("1997/09/30").getTime()/1000L);
				info.setHeight(168);
				info.setWeight(49);
				info.setOrientation(1);
				info.setEmotion(2);
			}
			users.set("zu", "template" + template);
			users.update();
			info.update();
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public void sms() throws ClientException {
		List<Users> users = Users.dao.find("select user_mobile from users where is_true = 1 and vip_end_time > 0 and up_time < 1547867408 and LENGTH(user_mobile) =11 ");
		for(Users one : users) {
			SendSmsResponse response = AliSMSUtil.sendSmsTemp(one.getUserMobile(),"SMS_182400228","五十度");
			System.out.println(response.getMessage());
		}
		renderText("OK");
	}
	
	public static void main(String[] args) throws ParseException {
		System.out.println(DateUtil.standardDateFormat.parse("1997/06/22").getTime()/1000L);
	}
}
